using UnityEngine;

public class LanguageController : MonoBehaviour
{
	private static LanguageController _instance;

	private SystemLanguage gameLanguage;

	private string gameLanguageString;

	private INIParser languageFile;

	private const string LANGUAGE_FILE_PREFIX = "Strings_";

	private const string LANGUAGE_KEY = "text";

	private const string LANGUAGE_DIRECTIORY = "Languages/";

	public static LanguageController instance
	{
		get
		{
			if (_instance == null)
			{
				FindOrCreateInstance();
			}
			return _instance;
		}
		private set
		{
			_instance = value;
		}
	}

	private static void FindOrCreateInstance()
	{
		LanguageController languageController = UnityEngine.Object.FindObjectOfType<LanguageController>();
		if (languageController != null)
		{
			instance = languageController;
		}
		else
		{
			CreateNewLanguageController();
		}
	}

	private static void CreateNewLanguageController()
	{
		GameObject gameObject = new GameObject("LanguageController");
		gameObject.AddComponent<LanguageController>();
		Object.DontDestroyOnLoad(gameObject);
	}

	public string GetStringWithNewLines(string sectionName)
	{
		try
		{
			string @string = GetString(sectionName);
			return @string.Replace('#', '\n');
		}
		catch
		{
			DebugUtil.Log("Error occurred while geting localized string with new line characters");
			return string.Empty;
		}
	}

	public string GetStringWithSpaces(string sectionName)
	{
		try
		{
			string @string = GetString(sectionName);
			return @string.Replace('^', ' ');
		}
		catch
		{
			DebugUtil.Log("Error occurred while geting localized string with space characters");
			return string.Empty;
		}
	}

	public string GetStringWithNewLinesAndSpaces(string sectionName)
	{
		try
		{
			string @string = GetString(sectionName);
			string text = @string.Replace('#', '\n');
			return text.Replace('^', ' ');
		}
		catch
		{
			DebugUtil.Log("Error occurred while geting localized string with new line and space characters");
			return string.Empty;
		}
	}

	public string GetString(string sectionName)
	{
		try
		{
			string text = languageFile.ReadValue(sectionName, "text", string.Empty);
			TestValueForEmptyString(text, sectionName);
			return text;
		}
		catch
		{
			DebugUtil.Log("Error occurred while geting localized string");
			return string.Empty;
		}
	}

	public string GetRichString(string sectionName)
	{
		string @string = GetString(sectionName);
		return @string.Replace("<br>", "\n");
	}

	protected void TestValueForEmptyString(string valueToTest, string sectionName)
	{
		if (valueToTest == string.Empty || valueToTest == " ")
		{
			DebugUtil.Log("MISSING LANG: sectionName=" + sectionName + " -> " + valueToTest);
		}
	}

	private void Awake()
	{
		if (_instance == null)
		{
			Initialize();
		}
	}

	private void Initialize()
	{
		_instance = this;
		InitializeGameLanguage();
		InitializeGameLanguageData();
	}

	private void InitializeGameLanguage()
	{
		gameLanguage = GameLanguage.GetSystemLanguageForDevice();
		gameLanguageString = gameLanguage.ToString();
		DebugUtil.Log("Język w którym wyświetlamy teksty: " + gameLanguageString);
	}

	private void InitializeGameLanguageData()
	{
		CloseCurrentLanguageFile();
		LoadCurrentLanguageIniParser();
	}

	private void CloseCurrentLanguageFile()
	{
		if (languageFile != null)
		{
			languageFile.Close();
			languageFile = null;
		}
	}

	private void LoadCurrentLanguageIniParser()
	{
		TextAsset currentLanguageTextAsset = GetCurrentLanguageTextAsset();
		languageFile = new INIParser();
		languageFile.Open(currentLanguageTextAsset);
	}

	private TextAsset GetCurrentLanguageTextAsset()
	{
		string currentLanguageFileName = GetCurrentLanguageFileName();
		return Resources.Load<TextAsset>("Languages/" + currentLanguageFileName);
	}

	private string GetCurrentLanguageFileName()
	{
		return "Strings_" + gameLanguageString;
	}
}
